package com.l.web.master.system.dictionary;

import com.l.web.WebConstant;
import org.apache.log4j.Logger;
import org.l.qiao.Cluster;
import org.l.qiao.Validate;
import org.l.qiao.annotation.Privilege;
import org.l.qiao.builder.Sort;
import org.l.qiao.util.ClusterUtil;
import org.l.qiao.web.KeyUpdate;
import com.l.web.common.ResponseCode;
import com.l.web.common.ResponseInfo;
import org.l.qiao.web.form.Form;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping(DictionaryController.URI)
public class DictionaryController {
    public static final String URI = WebConstant.MASTER_URI + "/system/dictionary";
    private static final Logger LOGGER = Logger.getLogger(DictionaryController.class);

    @Autowired
    private DictionaryService dictionaryService;

    @Privilege(module = "系统", function = "字典管理", operation = "查询")
    @RequestMapping("index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        modelMap.put(WebConstant.CONTROLLER_KEY, URI);
        return new ModelAndView(URI + "/index", modelMap);
    }

    @Privilege(module = "系统", function = "字典管理", operation = "查询")
    @RequestMapping("list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        Sort sort = new Sort();
        sort.asc(Dictionary.TITLE);
        List dictionarys = dictionaryService.getList(sort);
        modelMap.put("dictionarys", dictionarys);
        return new ModelAndView(URI + "/list", modelMap);
    }

    @Privilege(module = "系统", function = "字典管理", operation = "详情")
    @RequestMapping("detail")
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = new Form(request);
        int id = form.getInteger("id");
        LOGGER.debug("查看字典信息：id=" + id);
        if (id > 0) {
            Cluster dictionary = dictionaryService.detail(id);

            if (dictionary == null) {
                LOGGER.error("所查看的字典不存在");
                response.sendError(404);
                return null;
            }

            modelMap.put("dictionary", dictionary);
            modelMap.put("check", ClusterUtil.getCheck(Dictionary.class));
        } else {
            LOGGER.error("非法查看字典信息：id=" + id);
            response.sendError(404);
            return null;
        }

        return new ModelAndView(URI + "/detail", modelMap);
    }

    @Privilege(module = "系统", function = "字典管理", operation = "添加")
    @RequestMapping("add")
    public ModelAndView add(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
        modelMap.put("check", ClusterUtil.getCheck(Dictionary.class));
        return new ModelAndView(URI + "/add", modelMap);
    }

    @Privilege(module = "系统", function = "字典管理", operation = "添加")
    @RequestMapping("save")
    @ResponseBody
    public ResponseInfo save(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = new Form(request);
        Dictionary dictionary = new Dictionary();
        boolean result = form.getCluster(dictionary);
        LOGGER.debug("添加字典：" + dictionary);

        if (result) {
            int code = dictionaryService.save(dictionary);
            ResponseInfo responseInfo = new ResponseInfo(code);

            if (code == ResponseCode.FINISH) {
                responseInfo.put("id", dictionary.getId());
            }

            return responseInfo;
        } else {
            response.sendError(404);
            LOGGER.error("非法添加字典：" + dictionary);
            return null;
        }
    }

    @Privilege(module = "系统", function = "字典管理", operation = "修改")
    @RequestMapping("update")
    @ResponseBody
    public ResponseInfo update(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = new Form(request);
        KeyUpdate keyUpdate = form.getKeyUpdate(Dictionary.class);
        LOGGER.debug("修改信息信息：" + keyUpdate.toJson());
        if (keyUpdate.check()) {
            long code = ResponseCode.FAILURE;

            try {
                code = dictionaryService.update(keyUpdate);
            } catch (Exception e) {
                LOGGER.error("修改异常[" + keyUpdate.toJson() + "]：" + e);
            }

            return new ResponseInfo(code);
        } else {
            LOGGER.error("非法修改：" + keyUpdate.toJson());
            response.sendError(404);
            return null;
        }
    }

    @Privilege(module = "系统", function = "字典管理", operation = "修改")
    @RequestMapping("updateKey")
    @ResponseBody
    public ResponseInfo updateKey(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = new Form(request);
        int id = form.getInteger("id");
        String key = form.getString("key");
        String value = form.getString("value");

        long code = ResponseCode.FAILURE;

        try {
            code = dictionaryService.updateKey(id, key, value);
        } catch (Exception e) {
            LOGGER.error("修改异常：" + e);
        }

        return new ResponseInfo(code);
    }

    @Privilege(module = "系统", function = "字典管理", operation = "修改")
    @RequestMapping("removeKey")
    @ResponseBody
    public ResponseInfo removeKey(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = new Form(request);
        int id = form.getInteger("id");
        String key = form.getString("key");

        long code = ResponseCode.FAILURE;

        try {
            code = dictionaryService.removeKey(id, key);
        } catch (Exception e) {
            LOGGER.error("修改异常：" + e);
        }

        return new ResponseInfo(code);
    }

    @Privilege(module = "系统", function = "字典管理", operation = "删除")
    @RequestMapping("remove")
    @ResponseBody
    public ResponseInfo remove(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws IOException {
        Form form = new Form(request);
        List<String> ids = form.getList("id");
        LOGGER.debug("删除字典：id=" + ids);

        if (Validate.check(Validate.NUMBER, ids)) {
            int code = dictionaryService.remove(ids);
            return new ResponseInfo(code);
        } else {
            LOGGER.error("非法删除字典：id=" + ids);
            response.sendError(404);
            return null;
        }
    }

    @RequestMapping("item")
    @ResponseBody
    public ModelMap item(int id, ModelMap modelMap) throws IOException {
        Cluster dict = dictionaryService.detail(id);




    return modelMap;
    }
}
